package com.cloud.code.买票卖票;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * 描述:
 * 出售票的站点
 *
 * @author 54485
 * @create 2018-09-17 8:59
 */
public class SellShop implements Runnable{

    private Lock lock;

    private Condition condition;

    private static int tickets = 100;

    public SellShop(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    public void seller(){
        tickets --;
        System.out.println(Thread.currentThread().getName()+"卖出了1张票");

    }

    @Override
    public void run() {
        while (true){
            try {
                if(lock.tryLock(200, TimeUnit.SECONDS)){
                    if(tickets>0){
                        seller();
                        Thread.sleep(200);
                    }else {
                        System.out.println("票卖完了。。。");
                        break;
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }


        }
    }
}
